package com.cyan.note.config;

import com.cyan.note.security.JwtAuthenticationFilter;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration;
import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.web.cors.CorsConfigurationSource;

/**
 * Spring Security安全配置
 * 
 * @author ZhangzhenYu
 * @since 2025-01-13
 */
// @Configuration
// @EnableWebSecurity
// @EnableMethodSecurity(prePostEnabled = true)
// @RequiredArgsConstructor
public class SecurityConfig {
    
    // private final JwtAuthenticationFilter jwtAuthenticationFilter;
    // private final CorsConfigurationSource corsConfigurationSource;
    
    /**
     * 密码编码器
     */
    // @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }
    
    /**
     * 认证管理器
     */
    // @Bean
    public AuthenticationManager authenticationManager(AuthenticationConfiguration config) throws Exception {
        return config.getAuthenticationManager();
    }
    
    /**
     * 安全过滤链配置
     */
    // @Bean
    public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
        http
            // 禁用CSRF（因为使用JWT）
            .csrf(AbstractHttpConfigurer::disable)
            
            // 配置CORS - 临时禁用
            .cors(AbstractHttpConfigurer::disable)
            
            // 配置会话管理（无状态）
            .sessionManagement(session -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
            
            // 配置请求授权
            .authorizeHttpRequests(auth -> auth
                // 公开访问的接口
                .requestMatchers("/auth/**").permitAll()                    // 认证接口
                .requestMatchers("/test/**").permitAll()                    // 测试接口
                .requestMatchers("/swagger-ui/**").permitAll()              // Swagger UI
                .requestMatchers("/v3/api-docs/**").permitAll()             // API文档
                .requestMatchers("/favicon.ico").permitAll()               // 网站图标
                .requestMatchers("/error").permitAll()                     // 错误页面
                
                // 超级管理员专用接口
                .requestMatchers("/admin/**").hasRole("SUPER_ADMIN")
                
                // 店长专用接口
                .requestMatchers("/manager/**").hasRole("MANAGER")
                
                // 打手专用接口
                .requestMatchers("/worker/**").hasRole("WORKER")
                
                // API接口需要认证
                .requestMatchers("/api/**").authenticated()
                
                // 其他所有请求都需要认证
                .anyRequest().authenticated()
            );
            
            // 添加JWT过滤器
            // .addFilterBefore(jwtAuthenticationFilter, UsernamePasswordAuthenticationFilter.class);
        
        return http.build();
    }
}